package bigraph.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import bigraph.model.*;

public class SiteCreateCommand extends Command {

	private Root root;
	private Place place;
	private Site site;
	
	public SiteCreateCommand() {
	     super();
	     root = null;
	     place = null;
	     site = null;
	}
	public void setSite(Object s) {
	     if (s instanceof Site)
	            this.site = (Site)s;
	}
	public void setRoot(Object e) {
	     if (e instanceof Root)
	            this.root = (Root)e;
	     if (e instanceof Place)
	    	 	this.place = (Place)e;
	}
	public void setLayout(Rectangle r) {
	     if (site == null)
	            return;
	     site.setLayout(r);
	}
	@Override
	public boolean canExecute() {
	     if (site == null && (root == null || place == null))
	            return false;
	     return true;
	}

	@Override
	public void execute() {
		 if (root == null) {
			 place.addChild(site);
		 }
		 else
	     root.addChild(site);
	}
	
	@Override
	public boolean canUndo() {
	     if (site == null && (root == null || place == null))
	           return false;
	     if(root != null)
	     	return root.contains(site);
	     else
	    	 return place.contains(site);
	    	 
	}
	@Override
	public void undo() {
		 if (!root.removeChild(site));
		 place.removeChild(site);
	}

}
